package com.easondongh.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easondongh.business.domain.*;
import com.easondongh.business.mapper.*;
import com.easondongh.business.vo.GoodsBackQueryVo;
import com.easondongh.business.vo.GoodsSalesBackQueryVo;
import com.easondongh.common.vo.BasePageQueryVo;
import com.easondongh.common.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easondongh.business.service.GoodsSalesbackService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author EasonDongH
 * @date 2020/6/8 16:38
 */
@Service
public class GoodsSalesbackServiceImpl extends ServiceImpl<GoodsSalesbackMapper, GoodsSalesback> implements GoodsSalesbackService{

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsSalesMapper goodsSalesMapper;
    @Autowired
    private GoodsSalesbackMapper goodsSalesbackMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void processGoodsBack(GoodsSalesback goodsSalesback) {
        GoodsSales importData = this.goodsSalesMapper.selectById(goodsSalesback.getSalesId());
        importData.setNumber(Math.max(0, importData.getNumber() - goodsSalesback.getNumber()));
        this.goodsSalesMapper.updateById(importData);
        this.goodsMapper.updateStock(goodsSalesback.getGoodsId(), 0-goodsSalesback.getNumber());
        this.goodsSalesbackMapper.insert(goodsSalesback);
    }

    @Override
    public PageResult<GoodsSalesback> getByPage(BasePageQueryVo<GoodsSalesBackQueryVo> queryCondition) {
        IPage<GoodsSalesback> page = new Page<>(queryCondition.getPage(), queryCondition.getLimit());
        QueryWrapper<GoodsSalesback> queryWrapper = new QueryWrapper<>();
        GoodsSalesBackQueryVo queryParams = queryCondition.getQueryParams();
        if (queryParams != null) {
            queryWrapper.eq(queryParams.getGoodsId() != null, GoodsSalesback.COL_GOODS_ID, queryParams.getGoodsId());
            queryWrapper.eq(queryParams.getCustomerId() != null, GoodsSalesback.COL_CUSTOMER_ID, queryParams.getCustomerId());
            queryWrapper.gt(queryParams.getStartTime() != null, GoodsSalesback.COL_SALESBACK_TIME, queryParams.getStartTime());
            queryWrapper.lt(queryParams.getEndTime() != null, GoodsSalesback.COL_SALESBACK_TIME, queryParams.getEndTime());
        }
        queryWrapper.orderByDesc(GoodsSalesback.COL_SALESBACK_TIME);
        IPage<GoodsSalesback> iPage = this.goodsSalesbackMapper.selectPage(page, queryWrapper);
        List<GoodsSalesback> goodsSalesbackList = iPage.getRecords();
        if(!CollectionUtils.isEmpty(goodsSalesbackList)) {
            List<Customer> customerList = this.customerMapper.getAllEnableCustomer();
            List<Goods> goodsList = this.goodsMapper.getAllEnableGoods();
            Map<Integer, String> customerMap = new HashMap<>();
            Map<Integer, String> goodsMap = new HashMap<>();
            if(!CollectionUtils.isEmpty(customerList)) {
                customerMap = customerList.stream().collect(Collectors.toMap(Customer::getId, Customer::getCustomerName));
            }
            if(!CollectionUtils.isEmpty(goodsList)) {
                goodsMap = goodsList.stream().collect(Collectors.toMap(Goods::getId, Goods::getGoodsName));
            }
            for (GoodsSalesback goodsSalesback : goodsSalesbackList) {
                goodsSalesback.setGoodsName(goodsMap.get(goodsSalesback.getGoodsId()));
                goodsSalesback.setCustomerName(customerMap.get(goodsSalesback.getCustomerId()));
            }
        }

        return new PageResult<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), goodsSalesbackList);
    }
}
